package com.ruoyi.common.fegin;

import cn.hutool.core.convert.Convert;
import cn.hutool.json.JSON;
import cn.hutool.json.JSONUtil;
import com.alibaba.fastjson2.JSONObject;
import com.ruoyi.common.core.domain.model.LoginBody;
import com.ruoyi.common.fegin.entity.Authorization;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

@Data
@Slf4j
@Component
public class PortalRequisition {
    @Resource
    private PortalApi portalApi;

    public Authorization login(LoginBody loginBody) {
        JSON parse = JSONUtil.parse(loginBody);
        JSONObject jsonObject = null;
        try {
            jsonObject = portalApi.login(Convert.toStr(parse));
        } catch (Exception e) {
            log.error("请求失败", e);
            return null;
        }

        Authorization authorization = null;
        if (jsonObject != null) {
            authorization = com.alibaba.fastjson2.JSON.parseObject(com.alibaba.fastjson2.JSON.toJSONString(jsonObject), Authorization.class);
        }

        return authorization;
    }
}
